<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title></title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" th:href="@{/Hospital/Css/bootstrap.css}" />
    <link rel="stylesheet" type="text/css" th:href="@{/Hospital/Css/bootstrap-responsive.css}" />
    <link rel="stylesheet" type="text/css" th:href="@{/Hospital/Css/style.css}" />
    <script type="text/javascript" th:src="@{/Hospital/Js/jquery.js}"></script>
    <!--<script type="text/javascript" th:src="@{Js/jquery.sorted.js}"></script>-->
    <script type="text/javascript" th:src="@{/Hospital/Js/bootstrap.js}"></script>
    <script type="text/javascript" th:src="@{/Hospital/Js/ckform.js}"></script>
    <script type="text/javascript" th:src="@{/Hospital/Js/common.js}"></script>
    <script th:src="@{/Hospital/layer/layer.js}"></script>

 

    <style type="text/css">
        body {
            padding-bottom: 40px;
        }
        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }


    </style>
</head>
<body>
<form method="post" class="definewidth m20">
<input type="hidden" name="id" value="" />
    <table class="table table-bordered table-hover definewidth m10">
        <tr>
            <td width="10%" class="tableleft">资源名称</td>
            <td><input type="text" name="realname" id="menuName" value="挂号信息管理"/></td>
        </tr>
        <tr>
            <td class="tableleft">url</td>
            <td><input type="text" name="url" id="menuURL" value="query.action"/></td>
        </tr>
        <tr>
            <td class="tableleft">是否有效</td>
            <td>
                <input type="radio" id="sta1" name="status" value="0" checked/> 有效
                <input type="radio" id="sta2" name="status" value="1" /> 无效
            </td>
        </tr>
        <tr>
            <td class="tableleft"></td>
            <td>
                <button class="btn btn-primary" id="update" type="button">更新</button>&nbsp;&nbsp;
                <button type="button" class="btn btn-success" name="backid" id="backid">返回列表</button>
            </td>
        </tr>
    </table>
</form>
</body>
</html>
<script>
    //返回首页
    $(function () {       
		$('#backid').click(function(){
				window.location.href="index";
		 });
    });

    //url非空判断，鼠标移除输入框触发函数
    $("#menuURL").blur(function (){

        let menuURL = $("#menuURL").val();
        if (menuURL === ''){
            layer.msg("URL路径不能为空！")
        }
    })

    //解析url
    String.prototype.GetValue= function(para) {
        let reg = new RegExp("(^|&)"+ para +"=([^&]*)(&|$)");
        let r = this.substr(this.indexOf("\?")+1).match(reg);
        if (r!=null) return unescape(r[2]); return null;
    }
    //进页面默认执行
    $(function (){
        //获取url
        let url = document.location.toString();
        let id = url.GetValue("id");
        $.post("getInfoById", {id:id},function (data){
            let menuName = data.m_chineseName;
            let menuURL = data.m_name;
            let state = data.m_state;

            //填充数据
            $("input[name='id']").val(id);
            $("input[name='realname']").val(menuName);
            $("input[name='url']").val(menuURL);
            //选择框需要判断选中
            if (state === 0){
                $("#sta1").attr("checked","checked");
            }else {
                $("#sta2").attr("checked","checked");
            }

            let menuName1 = $("#menuName").val();
            //资源名唯一性验证，鼠标移除输入框触发函数
            $("#menuName").blur(function (){
                let menuName = $("#menuName").val();
                if (menuName1 !== menuName){
                    $.get("compare",{menuName:menuName},function (data){
                        //将服务器回复的结果进行显示
                        layer.msg(data.msg);
                    })
                }
            })

        })
    })

    //更新
    $(function () {
        $('#update').click(function(){

            //获取输入框最终数据
            let id = $("input[name='id']").val();
            let menuName = $("input[name='realname']").val();
            let menuURL = $("input[name='url']").val();
            let status = $("input:radio:checked").val();

            if(confirm("确认更新?")){
                //前端确认添加后，后端进行数据处理
                //先使用layer,弹出load（提示加载中...）
                const windowId = layer.load();//获取加载框
                $.post("update",{id:id,menuName:menuName,menuURL:menuURL,status:status},function (data){
                    //关闭load窗口
                    layer.close(windowId);
                    //将服务器回复的结果进行显示
                    layer.msg(data.msg);
                    if (data.status === 1){
                        window.location.href="index";
                    }

                })

            }
        });
    });

</script>